package frontend.components.base;

import backend.model.base.DBModel;
import frontend.model.base.UIModel;
import frontend.components.other.UserInputCallableAction;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.util.CollectionModel;

import java.util.Collections;
import java.util.Date;
import java.util.List;

public class ComponentFactory {
    public static <UIM extends UIModel> Panel createTextFieldComponent(String propertyId, String labelResourceKey, UIM model) {
        return new TextFieldComponent<UIM>(propertyId, labelResourceKey, model);
    }

    public static <UIM extends UIModel> Panel createPasswordFieldComponent(String propertyId, String labelResourceKey, UIM model) {
        return new PasswordFieldComponent<UIM>(propertyId, labelResourceKey, model);
    }

    public static <UIM extends UIModel> Panel createCheckboxComponent(String propertyId, String labelResourceKey, UIM model, UserInputCallableAction<Boolean> onClickAction) {
        return new CheckboxComponent<UIM>(propertyId, labelResourceKey, model, onClickAction);
    }

    public static <UIM extends UIModel> Panel createCheckboxComponent(String propertyId, String labelResourceKey, UIM model) {
        return new CheckboxComponent<UIM>(propertyId, labelResourceKey, model, null);
    }

    public static <T, UIM extends UIModel> Panel createDropDownChoiceComponent(String propertyId, String labelResourceKey, UIM uiModel,
                                                                               final List<T> availableOptions, IChoiceRenderer<T> choiceRender,
                                                                               boolean isNullValid) {
        return createDropDownChoiceComponent(propertyId, labelResourceKey, uiModel, new CollectionModel(availableOptions), choiceRender, isNullValid, null);
    }

    public static <T, UIM extends UIModel> Panel createDropDownChoiceComponent(String propertyId, String labelResourceKey, UIM uiModel,
                                                                               List<T> availableOptions, IChoiceRenderer<T> choiceRender,
                                                                               boolean isNullValid, UserInputCallableAction<T> onSelectAction) {
        return createDropDownChoiceComponent(propertyId, labelResourceKey, uiModel, new CollectionModel(availableOptions), choiceRender, isNullValid, onSelectAction);
    }

    public static <T, UIM extends UIModel> Panel createDropDownChoiceComponent(String propertyId, String labelResourceKey, UIM uiModel,
                                                                               CollectionModel availableOptionsCollectionModel, IChoiceRenderer<T> choiceRender,
                                                                               boolean isNullValid, UserInputCallableAction<T> onSelectAction) {
        return new DropDownComponent<UIM, T>(propertyId, labelResourceKey, uiModel, availableOptionsCollectionModel, choiceRender, isNullValid, onSelectAction);
    }

    public static <T1, T2, UIM extends UIModel> Panel createDoubleDropDownChoiceComponent(String componentId, String labelResourceKey, String propertyId1, String propertyId2,
                                                            UIM uiModel, final List<T1> availableOptions1, final List<T2> availableOptions2,
                                                            IChoiceRenderer<T1> choiceRender1, IChoiceRenderer<T2> choiceRender2,
                                                            boolean isNullValid, final UserInputCallableAction<T1> onSelectAction1, final UserInputCallableAction<T2> onSelectAction2) {
        return new DoubleDropDownComponent<UIM, T1, T2>(componentId, labelResourceKey, propertyId1, propertyId2, uiModel, availableOptions1, availableOptions2,
                choiceRender1, choiceRender2, isNullValid, onSelectAction1, onSelectAction2);
    }

    public static <DBM extends DBModel, UIM extends UIModel> PaletteComponent createPaletteComponent(String propertyId, UIM uiModel, List<DBM> availableItems,
                                                                                          int itemsNoToShow, boolean allowOrdering) {
        Collections.sort(availableItems);
        return new PaletteComponent<UIM, DBM>(propertyId, uiModel, availableItems, itemsNoToShow, allowOrdering);
    }

    public static <DBM extends DBModel, UIM extends UIModel> PaletteComponent createPaletteComponent(String propertyId, UIM uiModel, CollectionModel<DBM> availableItemsCollectionModel,
                                                                                          int itemsNoToShow, boolean allowOrdering) {
        return new PaletteComponent<UIM, DBM>(propertyId, uiModel, availableItemsCollectionModel, itemsNoToShow, allowOrdering);
    }

    public static <M>Panel createDatePickerComponent(String propertyId, String labelResourceKey, M model) {
        return new DatePickerComponent(propertyId, labelResourceKey, model);
    }

    public static <M>Panel createDatePickerComponent(String propertyId, String labelResourceKey, M model, UserInputCallableAction<Date> onSelectAction) {
        return new DatePickerComponent(propertyId, labelResourceKey, model, onSelectAction);
    }
}
